package Aleehot100;

/**
 * ClassName: c017
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/28 17:19
 * @Version 1.0
 * @Since 1.0
 */
public class c017 {
    public static void main(String[] args) {
        int[] arr = {3,2,4,5};
        System.out.println(firstMissingPositive(arr));
    }

    public static int firstMissingPositive(int[] nums) {

        int l = 0, r = nums.length - 1;
        while(l < r) {
            if(nums[l] == l + 1) {
                l ++;
            }
            //垃圾值
            else if(nums[l] < l + 1 || nums[l] >= r || nums[nums[l] - 1] == nums[l]) {
                swap(nums, l, -- r);
            } else {
                swap(nums, l, nums[l] - 1);
            }
        }
        return l + 1;
    }

    private static void swap(int[] arr, int l, int r) {
        if(l == r) {
            return;
        }
        arr[l] = arr[l] ^ arr[r];
        arr[r] = arr[l] ^ arr[r];
        arr[l] = arr[l] ^ arr[r];
    }
}
